package com.apache.flow.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.model.ActivityCoordinates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.apache.common.FlowBaseAction;
import com.apache.core.web.struts2.Struts2Utils;

@Namespace("/flow/core")
@Results({})
public class FlowViewPicAction extends FlowBaseAction {
	private Logger logger = LoggerFactory.getLogger(FlowViewPicAction.class);
	private static final long serialVersionUID = 1L;
	/**
	 * 流程引擎
	 */
	@Autowired
	private ProcessEngine processEngine;

	/**
	 * 活动节点坐标的集合
	 */
	private List<ActivityCoordinates> list_ac;

	/**
	 * 流程实例id
	 */
	private String processInsId;

	public String execute() {
		logger.info("获取当前执行节点坐标的方法开始执行,流程实例Id processInsId=" + processInsId);
		//管理流程定义
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//管理流程实例
		ExecutionService executionService = processEngine.getExecutionService();
		//流程实例
		ProcessInstance processInstance = executionService.findProcessInstanceById(processInsId);

		Set<String> activityNames = null;

		if (processInstance != null) {
			//活动节点名称
			activityNames = processInstance.findActiveActivityNames();
		}

		if (activityNames == null || activityNames.size() == 0) {
			return "end";
		} else {
			list_ac = new ArrayList<ActivityCoordinates>();
			Iterator<String> it = activityNames.iterator();
			while (it.hasNext()) {
				String activityName = it.next();
				ActivityCoordinates activitygCoordinates = repositoryService.getActivityCoordinates(
						processInstance.getProcessDefinitionId(), activityName);
				list_ac.add(activitygCoordinates);
			}
			return SUCCESS;
		}

	}

	/**
	 * 得到流程实例流程图
	 * @return String
	 * @throws IOException 
	 */
	public String getProcessPic() throws IOException {
		logger.info("查看流程图,流程实例Id processInsId=" + processInsId);
		//管理流程定义
		RepositoryService repositoryService = processEngine.getRepositoryService();
		//管理流程实例
		ExecutionService executionService = processEngine.getExecutionService();
		//流程实例
		ProcessInstance processInstance = executionService.findProcessInstanceById(processInsId);

		if (null == processInstance) {
			return null;
		}
		//流程定义ID
		String processDefinitionId = processInstance.getProcessDefinitionId();
		//流程定义
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				.processDefinitionId(processDefinitionId).uniqueResult();
		//流程发布ID
		String deploymentId = processDefinition.getDeploymentId();
		//获得该流程相关资源名称
		Set<String> resouceNames = repositoryService.getResourceNames(deploymentId);
		//流程图名称
		String picName = null;
		//TODO
		//目前认为取流程图片名称方法
		for (String name : resouceNames) {
			if (name.toLowerCase().endsWith(".png") || name.toLowerCase().endsWith(".tmp")) {
				picName = name;
				break;
			}
		}
		//获得流程图流
		InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, picName);
		byte[] b = new byte[1024];
		int len = -1;
		while ((len = inputStream.read(b, 0, 1024)) != -1) {
			Struts2Utils.getResponse().getOutputStream().write(b, 0, len);
		}
		return null;
	}

	public String getProcessInsId() {
		return processInsId;
	}

	public void setProcessInsId(String processInsId) {
		this.processInsId = processInsId;
	}

	public List<ActivityCoordinates> getList_ac() {
		return list_ac;
	}

	public void setList_ac(List<ActivityCoordinates> list_ac) {
		this.list_ac = list_ac;
	}

}
